package cn.sys.service.impl;

import cn.sys.controller.dto.MaintainDormitorySearchDTO;
import cn.sys.dao.MaintainDormitoryDao;
import cn.sys.entity.MaintainDormitory;
import cn.sys.service.MaintainDormitoryService;
import cn.sys.util.MybatisUtil;
import cn.sys.util.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MaintainDormitoryServiceImpl implements MaintainDormitoryService {
    private SqlSession sqlSession = MybatisUtil.getSQLSession();

    /**
     * 根据学生id得到的报修列表
     * @param searchDTO
     * @return
     */
    @Override
    public ResultData getMaintainDormitoryList(MaintainDormitorySearchDTO searchDTO) {
        MaintainDormitoryDao maintainDormitoryDao = sqlSession.getMapper(MaintainDormitoryDao.class);
        // 开启分页操作
        PageHelper.startPage(searchDTO.getPageNum(), searchDTO.getPageSize(), searchDTO.getOrderBy());
        // 查询
        List<MaintainDormitory> maintainDormitories = maintainDormitoryDao.findMaintainDormitoryByStuId(searchDTO);

        // 通过PageHepler构建的分页信息对象
        PageInfo<MaintainDormitory> pageInfo = PageInfo.of(maintainDormitories);

        /* 前端数据的构建：
               var maintainDormitories: {
							result: ture, // 返回状态
							msg: '',		// 返回信息
							pagination: { // 翻页数据
								currPage: 1,// 当前第几页
								total: 100, //数据总数
								pageSize: 10,  // 每页条数
							},
							data:[
							    {'title':'','':''},
							    {}
							]
						}
         */
        ResultData resultData = new ResultData();
        resultData.put("result", true);
        resultData.put("msg", "");
        // 构建翻页对象
        Map<String, Object> page = new HashMap<>();
        page.put("currPage", searchDTO.getPageNum());
        page.put("total", pageInfo.getTotal());
        page.put("pageSize", searchDTO.getPageSize());
        // 将翻页对象存入结果集
        resultData.put("pagination", page);

        // 由于maintainDormitories就是个对象，可以直接传入，但状态值需要在前端渲染一下
        resultData.put("datas", maintainDormitories);

        return resultData;
    }

    /**
     * 新增宿舍报修
     * @param stuId 传入的学生id
     * @param dorId 传入的宿舍id
     * @param imageUrls 传入的图片文件名的JSON数组，直接插入数据库
     * @param title 报修标题
     * @param desc  报修的详细描述
     * @return
     */
    @Override
    public ResultData addMaintainDormitory(Integer stuId, Integer dorId, String imageUrls, String title, String desc) {
        // 获取创建时间
        Date createTime = new Date();
        // 状态指定为0（未处理）
        int state = 0;
        // 封装到实体类中传给持久层
        MaintainDormitory maintainDormitory = new MaintainDormitory(stuId, dorId, desc, imageUrls, state, createTime, title);
        MaintainDormitoryDao maintainDormitoryDao = sqlSession.getMapper(MaintainDormitoryDao.class);
        int i = maintainDormitoryDao.addMaintainDormitory(maintainDormitory);
        if(i>0){
            return ResultData.ok(ResultData.OPERATION_SUCCESS, "添加成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL, "添加失败，请稍后再试");
    }

    /**
     * 根据维修状态获取维修列表信息及其对应的位置信息，是管理员部分的维修管理功能
     * @param searchDTO 搜索条件及分页信息
     * @return
     */
    @Override
    public ResultData getMaintainDormitoryListAndPositionByState(MaintainDormitorySearchDTO searchDTO) {
        MaintainDormitoryDao maintainDormitoryDao = sqlSession.getMapper(MaintainDormitoryDao.class);
        // 开启分页操作
        PageHelper.startPage(searchDTO.getPageNum(), searchDTO.getPageSize(), searchDTO.getOrderBy());
        // 查询
        List<Map<String, Object>> maintainDormitoryAndPosition = maintainDormitoryDao.findMaintainDormitoryAndPositionByState(searchDTO);
        // 通过PageHepler构建的分页信息对象
        PageInfo<Map<String, Object>> pageInfo = PageInfo.of(maintainDormitoryAndPosition);

        ResultData resultData = new ResultData();
        resultData.put("result", true);
        resultData.put("msg", "");
        // 构建翻页对象
        Map<String, Object> page = new HashMap<>();
        page.put("currPage", searchDTO.getPageNum());
        page.put("total", pageInfo.getTotal());
        page.put("pageSize", searchDTO.getPageSize());
        // 将翻页对象存入结果集
        resultData.put("pagination", page);

        resultData.put("datas", maintainDormitoryAndPosition);

        return resultData;
    }

    /**
     * 修改报修状态，并记录修改时间
     * @param id    报修id值
     * @param state 报修状态，0-未处理、1-处理中、2-已处理
     * @return
     */
    @Override
    public ResultData updateMaintainDormitoryState(Integer id, int state) {
        MaintainDormitoryDao maintainDormitoryDao = sqlSession.getMapper(MaintainDormitoryDao.class);
        Date updateTime = new Date();
        int i = maintainDormitoryDao.updateMaintainDormitoryStateById(id, state, updateTime);
        if(i>0){
            return ResultData.ok();
        }
        return ResultData.fail();
    }
}
